//
//  SetBacisViewController.swift
//  onemeetingmac
//
//  Created by 张衡 on 2020/6/1.
//  Copyright © 2020 onemeeting. All rights reserved.
//

import Foundation

class SetBacisViewController: BaseTRTCViewController {
    
    var titlesList:[String] = ["当前模式","其他","语言","您的姓名","会议密码","直播/录制"]
    var uCusViews:[CuSetView] = []
    var uObtns:[NSButton] = []
    
    override func loadView() {
        let view = NSView.init(frame: NSMakeRect(0, 0, kSet_BasicWidth, kSet_BasicHeight))
        view.wantsLayer = true
        view.layer?.backgroundColor = NSColor.white.cgColor
        self.view = view
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupUI()
        self.loadData()
        print("1324\(Td.sh().name)")
    }
    
    func loadData() {
        let defaultT = Td.sh().scene == .live ? "直播模式":"会议模式"
        self.popBtn.title = defaultT
        
    }
    
    func setupUI() {
        self.view.layer?.backgroundColor = color_gray(235).cgColor
        
        //初始化子类
        self.uCusViews = self.initViews(inView: self.view)
    }
    
    lazy var popBtn: NSPopUpButton = {
        let titles = ["会议模式","直播模式"]
        let btn = NSPopUpButton.init(title: "", target: self, action: #selector(popBtnAction(senber:)))
        btn.addItems(withTitles:titles)
        return btn
    }()
    
    lazy var langPopBtn: NSPopUpButton = {
        let btn = NSPopUpButton.init(title: "跟随系统", target: self, action: #selector(langPopBtnAction(senber:)))
        btn.addItems(withTitles: ["跟随系统","---","中文"])
        btn.menu?.needsUpdate()
        return btn
    }()
    
    lazy var nameInputView: SetInputView = {
        let view = SetInputView.init(text: Td.sh().name) { (string) in
            if let ownid = Td.sh().params.userId {
                NetEngine.reponse_updateSetup(ownid: ownid, fieldType: UpSetupType.name, value: string, success: { (_) in
                    UserInfoManager.shared.name = string
                }, fail: nil)
            }
        }
        view.maxCount = 20
        return view
    }()
    
    lazy var passwordInputView: SetInputView = {
        let view = SetInputView.init(text: Td.sh().cloud.password) { (string) in
            if let ownid = Td.sh().params.userId {
                NetEngine.reponse_updateSetup(ownid: ownid, fieldType: UpSetupType.password, value: string, success: { (_) in
                    Td.sh().cloud.password = string
                }, fail: nil)
            }
        }
        view.maxCount = 6
        view.isOnlyNumber = true
        return view
    }()
    
    lazy var oBtnView: NSView = {
        let view = NSView.init(frame: NSRect.zero)
        self.uObtns = self.initOBtns(inView: view)
        return view
    }()
    
    lazy var OpenLive: NSButton = {
        let btn = NSButton()
        btn.bezelStyle = .rounded
        btn.font = NSFont.systemFont(ofSize: 14)
        btn.title = "进入"
        btn.target = self
        btn.action = #selector(OpenLiveAction(senber:))
        return btn
    }()
    
    @objc func OpenLiveAction(senber:NSButton) {
        TrtcEngine.rtmpOrderFront(inWindow:self.view.window)
    }
    
    func initOBtns(inView:NSView) -> [NSButton] {
        var uBtns:[NSButton] = []
        let isVd = Td.sh().lc.isVdOpen
        let isMute = Td.sh().lc.isMute
        let btnTitles:[(String,Bool)] = [("保持摄像头关闭",!isVd),("不自动连接音频",isMute)]
        for (inx,val) in btnTitles.enumerated() {
            let btn = NSButton.init(title: val.0, target: self, action: #selector(oBtnAction(senber:)))
            btn.tag = 100+inx
            btn.setButtonType(NSButton.ButtonType.switch)
            btn.state = val.1 ? .on:.off
            inView.addSubview(btn)
            uBtns.append(btn)
            btn.snp.makeConstraints { (make) in
                make.width.equalToSuperview()
                make.left.equalToSuperview()
                make.top.equalTo(inx*34)
                make.height.equalTo(24)
            }
        }
        return uBtns
    }
    
    func initViews(inView:NSView) -> [CuSetView] {

        var uCusViews:[CuSetView] = []
        for (inx,val) in self.titlesList.enumerated() {
            var cusV:NSView?
            var cusSize:CGSize = CGSize.init(width: 180, height: 26)
            switch inx {
            case 0:
                cusV = self.popBtn
            case 1:
                cusV = self.oBtnView
                cusSize = CGSize.init(width: 180, height: 34*2)
            case 2:
                cusV = self.langPopBtn
            case 3:
                cusV = self.nameInputView
                cusSize = CGSize.init(width: 180, height: 40)
            case 4:
                cusV = self.passwordInputView
                cusSize = CGSize.init(width: 140, height: 40)
            case 5:
                cusV = self.OpenLive
                cusSize = CGSize.init(width: 90, height: 26)
            default:
                break
            }
            
            let cusView = CuSetView.init(text: val+" ", cusView: cusV, cusSize: cusSize)
            cusView.titleWidth = 170
            inView.addSubview(cusView)
            uCusViews.append(cusView)
            cusView.snp.makeConstraints { (make) in
                make.width.equalTo(cusSize.width+210)
                make.height.equalTo(cusSize.height)
                make.left.equalToSuperview().offset(20)
                if inx == 0 {
                    make.top.equalTo(30)
                }else {
                    make.top.equalTo(uCusViews[inx-1].snp.bottom).offset(20)
                }
                
            }
        }
        return uCusViews
    }
    
    @objc func popBtnAction(senber:NSPopUpButton) {
        senber.title = senber.selectedItem!.title
        UserInfoManager.shared.scene = senber.title=="会议模式" ? 0:1
    }
    
    @objc func langPopBtnAction(senber:NSPopUpButton) {
        senber.title = senber.selectedItem!.title
    }
    
    @objc func oBtnAction(senber:NSPopUpButton) {
        switch senber.tag {
        case 100:
            Td.sh().lc.isVdOpen = senber.state == .on ? false:true
        case 101:
            Td.sh().lc.isMute = senber.state == .on ? true:false
        default:
            break
        }
    }
}


extension NSMenu {
    func needsUpdate() {
        for (ix,mi) in self.items.enumerated() {
            if mi.title == "---" {
                self.removeItem(at: ix)
                self.insertItem(NSMenuItem.separator(), at: ix)
            }
        }
    }
}




